package system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import bean.CommentBean;
import dao.CommentDAO;
import dao.DAO;

/**
 * Class responsible for the operations of comments
 * @author Anne Lorayne, Laura Dutra, Otavio Lima
 */
public class Comment {

	private List<Comment> commentsList;

	private CommentBean comment;

	/**
	 * Constructor to a Comment
	 * @param id - id of comment
	 * @param text - text of comment
	 */
	public Comment(String owner, String id, String text, String login){
		this.comment = new CommentBean(owner, id, text, login);
		if(!CommentDAO.getInstance().contains(this.comment)){
			CommentDAO.getInstance().save(comment);
		}
		getCommentsByComment();


	}
	//TODO
	public boolean addCommentToComment(String text){
		return false;
	}

	/**
	 * Retrieves comments by a post
	 * @param postId - id of post
	 * @return list of comments of post
	 */
	public static List<Comment> getCommentsByPost(String postId){
		CommentBean comment;
		DAO<CommentBean> commentDAO;
		commentDAO = CommentDAO.getInstance();
		List<CommentBean> list = commentDAO.list();
		List<Comment> commentsByPost = new ArrayList<Comment>();
		for(Iterator<CommentBean> it = list.iterator(); it.hasNext();){
			comment = it.next();
			if(isCommentOfPost(comment.getId(), postId)){
				commentsByPost.add(new Comment(comment.getOwner(), comment.getId(), comment.getComment(), comment.getLogin()));

			}
		}
		return commentsByPost;
	}

	/**
	 * Returns id of comment
	 * @return id of comment 
	 */
	public String getID(){
		return comment.getId();
	}

	/**
	 * Returns text of comment
	 * @return text of comment
	 */
	public String getComment(){
		return comment.getComment();
	}
	
	/**
	 *Returns the comments of comments
	 *@return the comments of comments
	 */
	private void getCommentsByComment(){
		DAO<CommentBean> commentDAO;
		commentDAO = CommentDAO.getInstance();
		List<CommentBean> list = commentDAO.list();
		CommentBean bean;
		for(Iterator<CommentBean> it = list.iterator(); it.hasNext();){
			bean = it.next();
			if(isCommentOf(bean.getId())){
				commentsList.add(new Comment(bean.getOwner(), bean.getId(), bean.getComment(), bean.getLogin()));

			}
		}
	}

	/**
	 * Remove all comment of a post
	 * @param postId - id of post
	 * @return true if comments was removed
	 */
	public static boolean removeCommentsOfPost(String postId){
		DAO<CommentBean> commentDAO;
		commentDAO = CommentDAO.getInstance();
		List<CommentBean> list = commentDAO.list();
		CommentBean bean;
		for(Iterator<CommentBean> it = list.iterator(); it.hasNext();){
			bean = it.next();
			if(bean.getId().length() > postId.length())
				if (bean.getId().substring(0, postId.length()).equals(postId))
					if(!commentDAO.delete(bean))
						return false;;
		}

		return true;	
	}
	
	

	//TODO Change method name
	/**
	 * Returns boolean if is comment of id passed as parameter
	 * @param id
	 * @return
	 */
	private boolean isCommentOf(String id){
		if(id.length() > getID().length())
			if (id.substring(0, getID().length()).equals(getID()))
				if(id.substring(getID().length()).lastIndexOf("c") == 0)
					return true;
		return false;
	}

	/**
	 * Returns boolean if is comment of post passed as parameter
	 * @param commentId
	 * @param postId
	 * @return
	 */
	private static boolean isCommentOfPost(String commentId, String postId){
		if(commentId.length() > postId.length())
			if (commentId.substring(0, postId.length()).equals(postId))
				if(commentId.substring(postId.length()).lastIndexOf("c") == 0)
					return true;
		return false;
	}



	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Comment))
			return false;
		Comment other = (Comment) obj;
		if (comment == null) {
			if (other.comment != null)
				return false;
		} else if (!comment.equals(other.comment))
			return false;
		return true;
	}
	
	/**
	 * Delete the comment
	 */
	public void delete() {
		for (Iterator<Comment> it = commentsList.iterator(); it.hasNext();) 
			it.next().delete();
		CommentDAO.getInstance().delete(comment);
	}
	
	/**
	 * Returns the comment's author
	 * @return comment's author
	 */
	public String getAuthor() {
		return comment.getLogin();
	}

}
